package com.zrrd.javaSEPlus._04_reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author WanJl
 * @version 1.0
 * @title ReflectConstructor02
 * @description Constructor类创建对象的方式
 * @create 2023/11/12 15:03
 */
public class ReflectConstructor02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        method02();
    }
    public static void method01() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException{
        //1.先获取Class对象
        Class<?> clazz = Class.forName("com.zrrd.javaSEPlus._04_reflect.Student");
        //2.获取私有的有参构造方法，参数是String类型
        Constructor<?> constructor = clazz.getDeclaredConstructor(String.class);
        //我就犟，因为要强行的通过反射获取并使用，所以要临时取消访问检查。
        //暴力反射
        constructor.setAccessible(true);

        //3.通过constructor创建属于Student类的对象。
        Student s =(Student) constructor.newInstance("张三");
        System.out.println(s.getName());
    }
    public  static void method02() throws ClassNotFoundException, NoSuchMethodException {
        Class<?> clazz = Class.forName("com.zrrd.javaSEPlus._04_reflect.Student");
        Constructor<?> constructor = clazz.getConstructor();
        System.out.println(constructor);

    }
    public  static void method03(){

    }
}
